import Vue from 'vue'
import VueRouter from 'vue-router'
import config from './config'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: config
})

router.beforeEach(function(to, from, next){
	if(to.name == 'Login'){
		next();
	}else{
		if(to.meta.auth){
			const token = JSON.parse(sessionStorage.getItem('token'));
			if(token){
				if(token.level <= to.meta.level){
					next();
				}
			}else{
				next({name: 'Login'});
			}
		}else{
			next();
		}
	}
})

export default router
